package com.fastadmin.controller;

import cn.hutool.core.text.StrBuilder;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.fastadmin.config.security.annotation.AnonymousAccess;
import com.fastadmin.mapper.impl.XuQiuMapper;
import com.fastadmin.pojo.XuQiu;
import com.fastadmin.pojo.FastParam;
import com.google.common.collect.Maps;
import com.xphsc.easyjdbc.page.PageInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@Controller
@RequiredArgsConstructor
@Slf4j
public class XuQiuController extends BaseController {

    private final XuQiuMapper xuqiuMapper;
    HashMap<String, String> typeMap = new HashMap<String, String>() {{
        put("zdg", "钟点工");
        put("ys", "月嫂");
        put("dcbj", "单词保洁");
        put("yys", "育婴师");
        put("zjbm", "住家保姆");
        put("lrph", "老人陪护");
        put("crs", "催乳师");
        put("brph", "病人陪护");
    }};


    @GetMapping("sys/xuqiu/index")
    public String index() {
        return "sys/xuqiu/index";
    }

    @GetMapping("sys/xuqiu/add")
    public String add() {
        return "sys/xuqiu/add";
    }

    @GetMapping("sys/xuqiu/edit")
    public String index(@RequestParam Long ids, Model model) {
        XuQiu xuqiu = this.xuqiuMapper.findById(ids);
        model.addAttribute("xuqiu", xuqiu);
        return "sys/xuqiu/edit";
    }

    @RequestMapping("sys/xuqiu/data")
    @ResponseBody
    public Object data(FastParam fastParam) {
        PageInfo<XuQiu> pageInfo = this.xuqiuMapper.findByFastParam(fastParam);
        return datagrid(pageInfo);
    }

    @RequestMapping("sys/xuqiu/del")
    @ResponseBody
    public Object del(String ids) {
        String[] split = StrUtil.split(ids, ",");
        for (String s : split) {
            xuqiuMapper.deleteById(s);
        }
        return ok();
    }

    @RequestMapping("sys/xuqiu/save")
    @ResponseBody
    public Object save(XuQiu xuqiu) {
        if (ObjectUtil.isNotNull(xuqiu.getId())) {
            //修改
            xuqiuMapper.update(xuqiu);
        } else {
            //保存
            xuqiuMapper.save(xuqiu);
        }
        return ok();
    }

    @GetMapping("/f/xuqiu/list")
    @AnonymousAccess
    public String list() {
        return "front/xuqiu";
    }

    @PostMapping("/f/xuqiu/datagrid")
    @AnonymousAccess
    @ResponseBody
    public Object datagrid(Integer EndPos, Integer Step, Integer t) {
        PageInfo<XuQiu> pageInfo = xuqiuMapper.findByType(EndPos, Step, t);
        HashMap<Object, Object> resultMap = Maps.newHashMap();
        List<XuQiu> list = pageInfo.getList();
        StrBuilder html = StrBuilder.create();
        for (XuQiu xuQiu : list) {
            String type = typeMap.get(xuQiu.getType());
            html.append(" <li class='One_DingDanShow TimeOut'> ");
            html.append("                             <div class='one_title div_inline'> ");
            html.append("         " + type + " ");
            html.append("         </div> ");
            html.append("         <div class='two_time_and_id'> ");
            html.append("             发布时间：" + new DateTime(xuQiu.getCreate_time()).toString("yyyy-MM-dd HH:mm:ss") + "  ( 个人无佣金 ) ");
            html.append("         </div> ");
            html.append("         <div class='content_text'> ");
            html.append("             <img class='ico_dan' src='../217Dan_7DanList/DanICO.png'> ");
            html.append("             <div class='cont_in'> ");
            html.append("                                                 <onelinepx><span class='pr_oPx'>订单价格：</span><div class='contentPOX pr_oPx'> ");
            html.append("                                                             " + xuQiu.getMoney_other() + " ");
            html.append("                                                     </div> ");
            html.append("             </onelinepx> ");
            html.append("                                                                                             <onelinepx><span>服务内容：</span><div class='contentPOX'>日常家务, 家中有小孩</div></onelinepx> ");
            html.append("                                                     <onelinepx><span>工作时间：</span><div class='contentPOX'>一月休息" + xuQiu.getDays() + "天</div></onelinepx> ");
            html.append("                                                     <onelinepx><span>特别说明：</span><div class='contentPOX'>" + xuQiu.getRemarks() + "咨询电话" + xuQiu.getMobile() + "</div></onelinepx> ");
            html.append("                  ");
            html.append("             </div> ");
            html.append("         </div> ");
            html.append("             <div class='jiazheng_a'> ");
            html.append("                                                             <p1>客户称呼：" + xuQiu.getName() + "</p1> ");
            html.append("                     <!-- ");
            html.append("                                                                                                                    --> ");
            html.append("                                                     <p>"+xuQiu.getAddress()+"</p> ");
            html.append("             </div> ");
            html.append("         <!-- ");
            html.append("             <div class='jingao'> ");
            html.append("                 请确定您完全符合对应的要求，再联系。 ");
            html.append("             </div> ");
            html.append("         --> ");
            html.append("              <div class='bt_arry'> ");
            html.append("                                                                 <a class='dh on pingjia' data_aid='' data_fabuzhe='' href='javascript:void(0)'>需求已过期</a> ");
            html.append("                                               </div> ");
            html.append(" </li> ");
        }
        resultMap.put("EndPos", EndPos );
        resultMap.put("EndPos", Step + 10);
        resultMap.put("HasData", 10);
        resultMap.put("ayi_lie_List", html.toString());
        resultMap.put("re", "good");

        return resultMap;
    }

}
